#!/usr/bin/perl

#folders
$root_folder="/root/switch_project";
$result_folder="$root_folder/experiment/exp17"; #root dir to store experimental resutls
$aggregated_output = "$result_folder/aggregated_results.csv";


$runs=2; # number of repeated experiment (same setting)
#$num_of_flows=1; #number of flows to process

$flow2_sending_rate = 1000;
$flow2_rate = $flow2_sending_rate*1000; #1Gb/s

@speed;


#specify sending rate
$start_sending_rate=100;#Mb/s
$end_sending_rate=1000;
$sending_rate_gap=100;

#calculate speed and gap (gap is not used for now)
for($b=$start_sending_rate, $a=0; $b<=$end_sending_rate; $b+=$sending_rate_gap, $a++)
{
	$speed[$a]=$b;
}



open(OUTPUT_TOTAL, ">$aggregated_output") or die "$aggregated_output cannot be created.\n";
print OUTPUT_TOTAL "flow1_sending_rate, flow2_sending_rate, run#, flow#, avg_loss_rate, mean_delay, std_delay, jitter, mean_gap, std_gap\n";

for($j=0; $j<=$#speed; $j++)
{
for($a=0; $a<=$#speed; $a++)
{
	for($i=1; $i<=$runs; $i++)
	{
		chdir "$result_folder/$speed[$j]M/$speed[$a]M/$i";
		print "folder = $result_folder/$speed[$j]M/$speed[$a]M/$i\n";

		for($k=1; $k<=1; $k++)
		{
			$input = ($k).("_flow_table.csv");
			#print("dir = $result_folder/$speed[$j]M/$i, input = $input\n");
	
			$output = "results.txt";
			open(INPUT1, $input) or die "$input cannot be opened.\n";
			open(OUTPUT1, ">$output") or die "$output cannot be created.\n";

			system("$root_folder/script/process_delay_loss_table.pl $input $output");
			close(INPUT1);
			close(OUTPUT1);
		
			$input2 = "results.txt";
			open(INPUT2, $input2) or die "$input2 cannot be opened.\n";
			$line=<INPUT2>;
			chomp($line);
			print OUTPUT_TOTAL "$speed[$j], $speed[$a], $i, $k, $line\n";
		}
	}
}
}

chdir "$root_folder/script";
